package com.cc.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @User c.c
 * @Since 2023/2/13
 * @Time 16:08
 * @Description 驼峰下划线相互转换
 */

public class ConvertHumpUnderlinesToEachOtherUtil {


  /**
   * 驼峰转下划线
   *
   * @param str 传入值
   * @return 转换后的str
   * @author C.c
   * @since 2023-03-30 17:26
   **/
  public static String humpToUnderline(String str) {

    Pattern compile = compile("[A-Z]");

    Matcher matcher = compile.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
      matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
    }
    return matcher.appendTail(sb).toString();
  }

  /**
   * 下划线转驼峰
   *
   * @param str 传入值
   * @return 转换后的str
   * @author C.c
   * @since 2023-03-30 17:26
   **/

  public static String underlineToHump(String str, boolean isFirstBig) {
    String regex = "_(.)";
    Matcher matcher = compile(regex).matcher(str);
    while (matcher.find()) {
      String target = matcher.group(1);
      str = str.replaceAll("_" + target, target.toUpperCase());
    }
    if (isFirstBig) {
      return capitalizeFirstLetter(str);
    }
    return str;
  }

  public static String underlineToHump(String str) {
    return underlineToHump(str, false);
  }

  /**
   * 首字母大写
   *
   * @param
   * @return
   * @author C.c
   * @since 2023-04-08 13:17
   **/
  public static String capitalizeFirstLetter(String word) {
    return word.substring(0, 1).toUpperCase() + word.substring(1);
  }
}
